package com.raspbian.monitor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.google.gson.Gson;
import com.raspbian.monitor.entity.SysUser;
import com.raspbian.monitor.service.SysUserService;
import com.raspbian.monitor.configs.Const;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api/admin")
public class AdminController {
    @Autowired
    SysUserService sysUserService;

    /**
     * 列出用户信息
     *
     * @return
     */
    @GetMapping("/listAllUsers")
    public R listAllUsers() {
        try {
            return R.ok(sysUserService.list());
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("失败");
        }
    }

    /**
     * 删除用户信息
     */
    @GetMapping("/deleteUser")
    @Transactional(rollbackFor = Exception.class)
    public R deleteUser(Integer userId) {
        try {
            SysUser info = sysUserService.getOne(new QueryWrapper<SysUser>()
                    .eq("id", userId));
            if (info != null) {
                sysUserService.removeById(info.getId());
                return R.ok("成功");
            } else {
                return R.failed("失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("失败");
        }
    }


}
